package com.aner.tester.utils.text;

import java.util.Collection;
import java.util.Map;

/**
 * string utility class
 * 
 * @author aner
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils
{
    public static boolean isEmpty(Collection<?> coll)
    {
        return isNull(coll) || coll.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> coll)
    {
        return !isEmpty(coll);
    }

    public static boolean isEmpty(Object[] objects)
    {
        return isNull(objects) || (objects.length == 0);
    }

    public static boolean isNotEmpty(Object[] objects)
    {
        return !isEmpty(objects);
    }

    public static boolean isEmpty(Map<?, ?> map)
    {
        return isNull(map) || map.isEmpty();
    }

    public static boolean isNotEmpty(Map<?, ?> map)
    {
        return !isEmpty(map);
    }

    public static boolean isEmpty(String str)
    {
        return isNull(str) || EMPTY.equals(str.trim());
    }

    public static boolean isNotEmpty(String str)
    {
        return !isEmpty(str);
    }

    public static boolean isNull(Object object)
    {
        return object == null;
    }

    public static boolean isNotNull(Object object)
    {
        return !isNull(object);
    }

    public static boolean isArray(Object object)
    {
        return isNotNull(object) && object.getClass().isArray();
    }

    public static String substring(final String str, int start)
    {
        if (str == null)
        {
            return EMPTY;
        }

        if (start < 0)
        {
            start = str.length() + start;
        }

        if (start < 0)
        {
            start = 0;
        }
        if (start > str.length())
        {
            return EMPTY;
        }

        return str.substring(start);
    }

    public static String substring(final String str, int start, int end)
    {
        if (str == null)
        {
            return EMPTY;
        }

        if (end < 0)
        {
            end = str.length() + end;
        }
        if (start < 0)
        {
            start = str.length() + start;
        }

        if (end > str.length())
        {
            end = str.length();
        }

        if (start > end)
        {
            return EMPTY;
        }

        if (start < 0)
        {
            start = 0;
        }
        if (end < 0)
        {
            end = 0;
        }

        return str.substring(start, end);
    }

    public static String format(String template, Object... params)
    {
        if (isEmpty(params) || isEmpty(template))
        {
            return template;
        }
        return StringFormatter.format(template, params);
    }

    public static String replaceSingleQuoteWithDouble(String str) {
        return str.replaceAll("\'", "\"");
    }
}